package com.hc.server.actions;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.json.JSONArray;
import org.json.JSONException;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.db.DBFactory;
import com.hc.server.db.Mporder;

/**
 * 获取用户的订单记录
 * @author xujh
 *
 */
public class QUERY_ORDER implements IAction {

	private static final String OID = "OID";
	
	private static final String ORDER_ID = "OID";
	private static final String ORDER_DES = "DES";
	private static final String ORDER_STATUS = "STATUS";
	private static final String ORDER_AMOUNT = "AMOUNT";
	private static final String ORDER_CREATE_TIME = "CREATE_TIME";
	private static final String ORDER_REMARK = "REMARK";
	
	@Override
	public void execute(Request req, Response resp) {
		int oId;
		try {
			oId = req.getIntArg(OID);
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		
		Session session = DBFactory.openSession();
		Transaction t = session.beginTransaction();
		t.begin();
		Mporder mpo = (Mporder) session.load(Mporder.class, new Integer(oId));
		if(null != mpo){	
			resp.appendRespContent(ORDER_ID, mpo.getId());
			resp.appendRespContent(ORDER_AMOUNT, mpo.getAmount());
			resp.appendRespContent(ORDER_STATUS, mpo.getStatus());
			try {
				String des = mpo.getDes();
				resp.appendRespContent(ORDER_DES, new JSONArray(des));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			resp.appendRespContent(ORDER_CREATE_TIME, mpo.getCreateTime());
			try {
				String remark = mpo.getRemark();
				if(null != remark){
					resp.appendRespContent(ORDER_REMARK, new JSONArray(remark));
				}else{
					resp.appendRespContent(ORDER_REMARK, new JSONArray());
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}		
		}else{
			resp.setErrResp(Error.ORDER_NOFOUND_ERR_CD, Error.ORDER_NOFOUND_ERR_MSG);			
		}
		session.close();
	}

}
